package com.example.mqspring.rabiitmqbasicleverone.rabbitmq;

import com.example.mqspring.entity.Book;
import com.rabbitmq.client.Channel;
import com.rabbitmq.tools.json.JSONWriter;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示JSON格式发送消息
 */
public class ProductJson extends Product {

    @Override
    public void sendMessage(Channel channel, String myQueue) {

        List<Book> books = new ArrayList<>();
        for (int i = 0; i<11; i++) {
            Book book = new Book();
            book.setAuthor("bobo"+i);
            book.setDescripe("good book"+i);
            book.setId(i);
            book.setName("天龙八部");
            books.add(book);
        }
        JSONWriter jsonWriter = new JSONWriter();
        String jsonMessage = jsonWriter.write(books);
        try {
            channel.basicPublish("",myQueue,null,jsonMessage.getBytes("UTF-8"));
        } catch (Exception e) {
            System.out.println("异常");
        }
    }

}
